//
//  GYSpecialViewController.m
//  GoodLuck
//
//  Created by apple on 2018/5/14.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//


static NSString *shopCellIdentifier = @"shopCellIdentifier";

#import "GYSpecialViewController.h"
#import "GYNetWorkTools.h"
#import "GYShopCell.h"
#import "GYHome.h"
#import "GYShopHomeViewController.h"
#import "ZXCycleScrollView.h"
#import "GYBanner.h"


@interface GYSpecialViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/** 首页数据模型 */
@property (nonatomic, strong) GYHome *homeModel;

/** cell缓存*/
@property (nonatomic, strong) NSMutableDictionary *cachesHeight;

@end

@implementation GYSpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"专题";

    //注册cell
    [self.collectionView registerClass:[GYShopCell class] forCellWithReuseIdentifier:shopCellIdentifier];
    
    //下拉刷新
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupHomeData)];

    //加载数据
    [self setupHomeData];
}

- (void)setupHomeData {
   [GYHome loadSpecial:self.special_id finish:^(GYHome * _Nullable model, NSError * _Nullable error) {
       [self.collectionView.mj_header endRefreshing];
       if (model != nil) {
           
           //1.刷新数据
           self.homeModel = model;
           [self.collectionView reloadData];
           
       }
   }];
}

#pragma mark -- UICollectionViewDataSource and UICollectionViewDelegate and UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.homeModel.home_Goods.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    GYShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:shopCellIdentifier forIndexPath:indexPath];
    cell.goods = self.homeModel.home_Goods[indexPath.item];
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    GYShopHomeViewController *shopController = [[GYShopHomeViewController alloc] init];
    shopController.hidesBottomBarWhenPushed = YES;
    GYGoods *goods = self.homeModel.home_Goods[indexPath.item];
    shopController.goods_id = goods.goods_id;
    [self.navigationController pushViewController:shopController animated:YES];

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    CGFloat height = CGFLOAT_MIN;
//    for (GYGoods *model in self.homeModel.home_Goods) {
//        CGFloat rowHeight = [model getHeight];
//        if (rowHeight > height) {
//            height = rowHeight;
//        }
//    }
//
//    return CGSizeMake((ScreenWidth-30)/2, height);
    GYGoods *goods = self.homeModel.home_Goods[indexPath.item];

    //1.首选从缓存中取
    CGFloat height = [self.cachesHeight[goods.goods_id] floatValue];
    if (height != 0) {
        return CGSizeMake((ScreenWidth-30)/2, height);
    }

    //没有高度
    CGFloat rowHeight = [goods getHeight];
    [self.cachesHeight setObject:@(rowHeight) forKey:goods.goods_id];
    return CGSizeMake((ScreenWidth-30)/2, [goods getHeight]);

}

@end
